<?php

	namespace app\jobs;

	use think\facade\Log;
	use think\queue\Job;

	class BasicJobs
	{

		//执行方法
		protected $action;

		//轮询次数
		protected $attempts = 3;


		public function __call ($name, $arguments) {
			$this->action = $name;
			$this->fire(...$arguments);
		}

		public function fire (Job $job, $data) {
			try {
				//默认执行doJob方法
				$action = $this->action ? $this->action : 'doJob';
				if (method_exists($this, $action)) {
					if ($this->{$action}($data)) {
						$job->delete();
					} else {
						if ($job->attempts() > $this->attempts) {
							$job->delete();
						}
					}
				} else {
					$job->delete();
					Log::error('方法：' . $action . '，不在类：' . __CLASS__ . '中');
				}
			} catch (\Exception $e) {
				$job->delete();
				Log::error('执行消息队列发生错误,错误原因：' . $e->getMessage());
			}
		}

		/**
		 * 任务失败执行方法
		 *
		 * @param $data
		 * @param $e
		 */
		public function failed ($data, $e) {

		}


	}
